<?php
namespace App\Service;
use App\Common\Utill\OperationLog;
use App\Models\WebsitesAdmin;
use App\Models\WebsiteQuick;
use Illuminate\Support\Facades\DB;

class WebsiteQuickService extends CommonService{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 列表数据
     * @param $request
     * @param $num
     * @return array|mixed
     */
    public function getListData($request, $num)
    {
        $model = new WebsiteQuick();
        $query = $model::query();
        $query->where('is_del',1);
        //网址
        $websiteId = $request->get('website_id',"");
        $query->where("website_id",$websiteId);
        //标题
        $title = $request->input('title', '');
        if ($title) {
            $query->where('names', 'like', '%' . $title . '%');
        }
        //状态
        $status = $request->input('status', '');
        if ($status) {
            $query->where('status', $status);
        }
        $query->orderBy('sorts', 'asc');
        $query->orderBy('id', 'desc');
        $result = $query->paginate($num)->toArray();
        foreach ($result['data'] as $key => $val) {
            $result['data'][$key]['statusName'] = $val['status'] == 1 ? '显示' : '隐藏';
        }
        return $this->success('SUCCESS', $result['total'], $result['data']);
    }

    /**
     * 添加保存
     * @param $names
     * @param $domains
     * @return array|mixed
     */
    public function store($names, $jumpUrl,$sorts,$website_id)
    {
        $postData['names'] = $names;
        $postData['jump_url'] = $jumpUrl ?? '';
        $postData['sorts'] = $sorts ?? 1;
        $postData['website_id'] = $website_id;
        $postData['status'] = 1;
        $postData['is_del'] = 1;
        $postData['created_at'] = date('Y-m-d H:i:s');
        $lastId = WebsiteQuick::query()->insertGetId($postData);
        if(!$lastId){
            return $this->error('添加失败');
        }
        OperationLog::record(1,'快速连接-添加',$lastId,$postData);
        return $this->success('添加成功');
    }

    /**
     * 编辑保存
     * @param $title
     * @param $domains
     * @return array|mixed
     */
    public function update($id, $names, $jumpUrl,$sorts)
    {
        $oldData = WebsiteQuick::query()->where('id',$id)->first()->toArray();
        $postData['names'] = $names;
        $postData['jump_url'] = $jumpUrl ?? '';
        $postData['sorts'] = $sorts ?? 1;
        $postData['updated_at'] = date('Y-m-d H:i:s');
        $result = WebsiteQuick::query()->where('id', $id)->update($postData);
        if(!$result){
            return $this->error('编辑失败');
        }
        OperationLog::record(2,'快速连接-编辑',$id,$postData,$oldData);
        return $this->success('编辑成功');
    }

    /**
     * 状态修改
     * @return void
     */
    public function status($id)
    {
        $isShow = WebsiteQuick::query()->where(['id'=>$id])->value('status');
        $data['status'] = $isShow == 1 ? -1 : 1;
        $data['updated_at'] = date('Y-m-d H:i:s');
        $result = WebsiteQuick::query()->where('id', $id)->update($data);
        if (!$result) {
            return $this->error('设置失败');
        }
        OperationLog::record(2,'快速连接-状态编辑',$id,['status'=>$data['status']],['status'=>$isShow]);
        return $this->success('设置成功');
    }

    /**
     * 单个删除
     */
    public function delOne($id)
    {
        $result = WebsiteQuick::findOrFail($id);
        $result->is_del = -1;
        $result->updated_at = date("Y-m-d H:i:s");
        if (!$result->save()) {
            return $this->error('删除失败');
        }
        //
        OperationLog::record(3,'快速连接-删除',$id);
        return $this->success("删除成功");
    }
}
